package com.friendfeed.api;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class User {
    private String id;
    private String name;
    private String nickname;
    private String profileUrl;
    private List<UserService> services;
    private List<User> subscriptions;
    private List<Room> rooms;

    public User() {
    }

    public User(JSONObject jsonObject) throws JSONException {
        id = jsonObject.getString("id");
        name = jsonObject.getString("name");
        if (jsonObject.has("nickname") && !jsonObject.isNull("nickname")) {
            nickname = jsonObject.getString("nickname");
        }
        profileUrl = jsonObject.getString("profileUrl");

        if (jsonObject.has("services")) {
            JSONArray servicesArray = jsonObject.getJSONArray("services");
            services = new ArrayList<UserService>(servicesArray.length());
            for (int i = 0; i < servicesArray.length(); i++) {
                services.add(new UserService(servicesArray.getJSONObject(i)));
            }
        }

        if (jsonObject.has("subscriptions")) {
            JSONArray subscriptionsArray = jsonObject.getJSONArray("subscriptions");
            subscriptions = new ArrayList<User>(subscriptionsArray.length());
            for (int i = 0; i < subscriptionsArray.length(); i++) {
                subscriptions.add(new User(subscriptionsArray.getJSONObject(i)));
            }
        }

        if (jsonObject.has("rooms")) {
            JSONArray roomsArray = jsonObject.getJSONArray("rooms");
            rooms = new ArrayList<Room>(roomsArray.length());
            for (int i = 0; i < roomsArray.length(); i++) {
                rooms.add(new Room(roomsArray.getJSONObject(i)));
            }
        }
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getNickname() {
        return nickname;
    }

    public String getProfileUrl() {
        return profileUrl;
    }

    public List<UserService> getServices() {
        return services;
    }

    public List<User> getSubscriptions() {
        return subscriptions;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public void setProfileUrl(String profileUrl) {
        this.profileUrl = profileUrl;
    }

    public void setServices(List<UserService> services) {
        this.services = services;
    }

    public void setSubscriptions(List<User> subscriptions) {
        this.subscriptions = subscriptions;
    }

    public List<Room> getRooms() {
        return rooms;
    }

    public void setRooms(List<Room> rooms) {
        this.rooms = rooms;
    }

    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                ", profileUrl='" + profileUrl + '\'' +
                '}';
    }
}
